
package insert.personinfo;

import model.bean.PersonInfo;
import model.dao.DistrictDAO;
import model.dao.PersonInfoDAO;

import org.hibernate.Session;

import com.e885.hibernate.AbstractInsertion;
import com.e885.util.date.DateUtil;
import com.e885.util.string.StringUtil;

/**
 * 新增 Table PERSON_INFO 的測試資料
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */
public class PersonInfoInsertion extends AbstractInsertion
{
	private static int idx = 1000;

	public static void main( String[] args )
	{
		new PersonInfoInsertion( idx ).insertTestData();
	}

	public PersonInfoInsertion()
	{
	}

	public PersonInfoInsertion( int insertionNumber )
	{
		super( insertionNumber );
	}

	@Override
	public void insertTestData( Session session )
	{
		PersonInfoDAO infoDao = null;
		DistrictDAO districtDao = null;
		try
		{
			infoDao = new PersonInfoDAO();
			districtDao = new DistrictDAO();

			String temp;
			for( int i = 0; i < insertionNumber; i++ )
			{
				temp = StringUtil.pendingZero( ( i + 1 ), 6 );

				PersonInfo personInfo = new PersonInfo();
				personInfo.setIdentity( StringUtil.fetchRandomIdentity() );
				personInfo.setName( "姓名_" + temp );
				personInfo.setSex( StringUtil.fetchRandomSex() );
				personInfo.setBirthDate( DateUtil.fetchRandomDate( 1948 ) );
				personInfo.setDistrict( districtDao.findRandomDistrict() );
				personInfo.setAddress( "地址_" + temp );

				infoDao.save( personInfo );

				if( i % 25 == 0 )
				{
					session.flush();
					session.clear();
				}
			}
		}
		finally
		{
			infoDao = null;
			districtDao = null;
		}
	}
}
